package base.多线程.同步代码.练习;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;

public class Cjiangxing extends Thread{
    static ArrayList<Integer> list = new ArrayList<>();

    //static ArrayList<Integer> T1 = new ArrayList<>();
    //static ArrayList<Integer> T2 = new ArrayList<>();

    static {
        Collections.addAll(list,10,20,45,50,100,500,700,900,1000);
    }

    @Override
    public void run() {
        /*线程栈*/
        ArrayList<Integer> boxlist = new ArrayList<>();
        while (true){

            synchronized(Cjiangxing.class){
                if (list.size() == 0){
//                    if ("抽奖箱1：".equals(getName())){
//                        int max = getMax(T1);
//                        int count = getCount(T1);
//                        System.out.println(getName()+T1+",最高奖项金额为“"+max+"”,总计金额为“"+count+"”");
//                    }else {
//                        int max = getMax(T2);
//                        int count = getCount(T2);
//                        System.out.println(getName()+T2+",最高奖项金额为“"+max+"”,总计金额为“"+count+"”");
//                    }

                    int max = getMax(boxlist);
                    int count = getCount(boxlist);
                    System.out.println(getName()+boxlist+",最高奖项金额为“"+max+"”,总计金额为“"+count+"”");


                    break;
                }else {
                    Random r = new Random();
                    int index = r.nextInt(list.size());
                    int price = list.remove(index);
                    //System.out.println(getName()+"抽取到了"+price+"元");

//                    if ("抽奖箱1：".equals(getName())){
//                        T1.add(price);
//                    }else {
//                        T2.add(price);
//                    }
                    boxlist.add(price);
                }
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private Integer getMax(ArrayList<? super Integer> list){ //需要传一个E类型或者E类型的超类
        int max = (int) list.get(0);
        for (Object i : list){
            if ((int) i > max){
                max = (int) i;
            }
        }
        return max;
    }

    private Integer getCount(ArrayList<Integer> list){
        int count = 0;
        for (Integer i : list){
            count += i;
        }
        return count;
    }
}
